package userspecctrl

import (
	"net/http"
	"strconv"
	"web_server/model/mainmodel"
	"web_server/model/userspecmodel"
	"web_server/utils"

	"github.com/gin-gonic/gin"
)

func UserSpecAdd(ctx *gin.Context, user mainmodel.UserModel) {
	ctx.HTML(http.StatusOK, "userspec-add.html", gin.H{
		"pubkey": utils.GetPubKeyString(),
	})
}

func UserSpecLst(ctx *gin.Context, user mainmodel.UserModel) {
	pageStr := ctx.Query("page")
	var page int64 = 1
	if n, err := strconv.ParseInt(pageStr, 10, 64); err != nil && n > 0 {
		page = n
	}
	if nameCodeStr, ok := ctx.GetQuery("namecode"); ok && nameCodeStr != "" {
		userspecList, pagemax := userspecmodel.GetUserSpecListByCode(nameCodeStr, page)
		if page > pagemax {
			page = pagemax
		}
		ctx.HTML(http.StatusOK, "userspec-list.html", gin.H{
			"usl":      userspecList,
			"page":     page,
			"last":     page - 1,
			"next":     page + 1,
			"pagemax":  pagemax,
			"pubkey":   utils.GetPubKeyString(),
			"namecode": nameCodeStr,
			"time":     "",
			"params":   "&namecode=" + nameCodeStr,
		})
		return
	} else if timeStr := userspecmodel.TimedateValid(ctx.GetQuery("time")); timeStr != nil {
		userspecList, pagemax := userspecmodel.GetUserSpecListByTime(*timeStr, page)
		if page > pagemax {
			page = pagemax
		}
		ctx.HTML(http.StatusOK, "userspec-list.html", gin.H{
			"usl":      userspecList,
			"page":     page,
			"last":     page - 1,
			"next":     page + 1,
			"pagemax":  pagemax,
			"pubkey":   utils.GetPubKeyString(),
			"namecode": "",
			"time":     timeStr,
			"params":   "&timeStr=" + *timeStr,
		})
		return
	} else {
		userspecList, pagemax := userspecmodel.GetUserSpecList(page)
		if page > pagemax {
			page = pagemax
		}
		ctx.HTML(http.StatusOK, "userspec-list.html", gin.H{
			"usl":      userspecList,
			"page":     page,
			"last":     page - 1,
			"next":     page + 1,
			"pagemax":  pagemax,
			"pubkey":   utils.GetPubKeyString(),
			"namecode": "",
			"time":     "",
			"params":   "",
		})
	}
}

func UserSpecEdt(ctx *gin.Context, user mainmodel.UserModel) {
	editIdStr := ctx.Query("id")
	editId := user.Id
	if id, err := strconv.ParseInt(editIdStr, 10, 64); err == nil && id != 0 {
		editId = id
	}
	if editUser := userspecmodel.GetUserSpecModel(editId); editUser != nil {
		ctx.HTML(http.StatusOK, "userspec-edit.html", gin.H{
			"usm":    *editUser,
			"pubkey": utils.GetPubKeyString(),
		})
	} else {
		ctx.String(http.StatusOK, "没有此用户!")
	}
}

func UserSpecAddPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	var jsdata userspecmodel.UserSpecModel
	if err := utils.B64RsaJsonDecrypt(r.FormValue("data"), &jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	time := userspecmodel.TimedateValid(jsdata.Timedate, true)
	if len(jsdata.Namecode) < 5 || len(jsdata.Namecode) > 10 ||
		len(jsdata.Specname) < 2 || len(jsdata.Specname) > 15 ||
		time == nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	jsdata.Timedate = *time
	if err := userspecmodel.SetUserSpecAdd(jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "创建失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}

func UserSpecEdtPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	var jsdata userspecmodel.UserSpecModel
	if err := utils.B64RsaJsonDecrypt(r.FormValue("data"), &jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "用户名密码格式错误",
		})
		return
	}
	if err := userspecmodel.SetUserSpecEdit(jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "创建失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}

func UserSpecDelPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	data := r.FormValue("data")
	var jsdata struct {
		Ids []int `json:"id"`
	}
	if err := utils.B64RsaJsonDecrypt(data, &jsdata); err != nil || len(jsdata.Ids) == 0 || len(jsdata.Ids) >= 20 {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
	}
	if err := userspecmodel.SetUserSpecDel(jsdata.Ids); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "删除失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}
